<template>
  <div class="tenant-container">
    <ContentWrap>
      <el-form
        ref="queryFormRef"
        class="-mb-15px"
        :model="queryParams"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="会员ID" prop="userId">
          <el-input v-model="queryParams.userId" placeholder="请输入会员ID" class="!w-240px" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="queryParams.mobile" placeholder="请输入手机号" class="!w-240px" />
        </el-form-item>
        <el-form-item label="时间" prop="createTime">
          <el-date-picker
            v-model="queryParams.createTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="datetimerange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-240px"
            :shortcuts="shortcuts"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <el-tag size="large" class="mb-15px"
      >记录列表 检测总次数：{{ totalTenantReportCount }}次</el-tag
    >
    <ContentWrap>
      <el-table :data="tableData" :stripe="true" :show-overflow-tooltip="true" :loading="loading">
        <el-table-column
          label="时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
          width="180"
        />
        <el-table-column label="会员Id" prop="userId" align="center" />
        <el-table-column label="会员手机号" prop="mobile" align="center" />
        <el-table-column label="分数" prop="score" align="center" />
        <el-table-column label="年龄" prop="age" align="center" />
        <el-table-column label="所属门店" prop="storeName" align="center" />
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button type="primary" link @click="showTongueReport(scope.row)">查看报告</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
    <Modal
      v-model:visible="tongueReportVisible"
      title=""
      width="420px"
      :show-close="true"
      :close-on-click-modal="false"
      custom-class="tongue-report-modal"
    >
      <TongueReport v-if="tongueReportVisible" :report-data="tongueReportData" />
    </Modal>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, toRefs } from 'vue'
import { dateFormatter } from '@/utils/formatTime'
import { TongueRecordApi } from '@/api/application/tongue/record'
import TongueReport from '@/components/TongueReport/index.vue'
import * as MemberReportApi from '@/api/member/report'
const tongueReportVisible = ref(false)
const tongueReportData = ref({})
const message = useMessage()
import dayjs from 'dayjs'
const shortcuts = [
  {
    text: '今日',
    value: () => {
      const today = dayjs().format('YYYY-MM-DD')
      return [today + ' 00:00:00', today + ' 23:59:59']
    }
  },
  {
    text: '昨日',
    value: () => {
      const yesterday = dayjs().subtract(1, 'day').format('YYYY-MM-DD')
      return [yesterday + ' 00:00:00', yesterday + ' 23:59:59']
    }
  },
  {
    text: '近7日',
    value: () => {
      const end = dayjs().format('YYYY-MM-DD')
      const start = dayjs().subtract(6, 'day').format('YYYY-MM-DD')
      return [start + ' 00:00:00', end + ' 23:59:59']
    }
  },
  {
    text: '近30日',
    value: () => {
      const end = dayjs().format('YYYY-MM-DD')
      const start = dayjs().subtract(29, 'day').format('YYYY-MM-DD')
      return [start + ' 00:00:00', end + ' 23:59:59']
    }
  }
]
const state = reactive({
  queryParams: {
    userId: '',
    mobile: '',
    createTime: [],
    pageNo: 1,
    pageSize: 10
  },
  totalTenantReportCount: 0,
  loading: false,
  tableData: [],
  total: 0
})
const { queryParams, loading, tableData, total, totalTenantReportCount } = toRefs(state)
const queryFormRef = ref() // 搜索的表单

const handleQuery = () => {
  getList()
}
const handleReset = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await TongueRecordApi.getReportPage(queryParams.value)
    tableData.value = data.pageResult.list
    total.value = data.pageResult.total
    totalTenantReportCount.value = data.totalTenantReportCount
  } finally {
    loading.value = false
  }
}
const showTongueReport = (row: MemberReportApi.MemberReportVO) => {
  try {
    // 解析JSON字符串
    const data = row.report
    let reportData
    if (data.includes('.pdf')) {
      reportData = data.replace(/^"|"$/g, '')
    } else {
      reportData = JSON.parse(data)
    }
    tongueReportData.value = reportData
    tongueReportVisible.value = true
  } catch (error) {
    console.error('解析报告数据失败:', error)
    message.error('报告数据格式错误')
  }
}
onMounted(() => {
  getList()
})
const handleRecord = (row?: any) => {
  console.log(row)
}
</script>

<style scoped lang="scss"></style>
